" Long Live the Command Line!
" {{{ Plugins First
set nocompatible
filetype off
call plug#begin('~/.vim/plugged')
Plug 'junegunn/vim-plug'
Plug 'wakatime/vim-wakatime'
" Plug 'lambdalisue/suda.vim'
Plug 'preservim/nerdcommenter'  
" Plug 'udalov/kotlin-vim'
" Plug 'vim-scripts/Solarized'
" Plug 'reedes/vim-colors-pencil'
" Plug 'itchyny/landscape.vim'
" Plug 'Raimondi/delimitMate'
" Plug 'neoclide/coc.nvim', {'branch': 'release'} " Use release branch (recommend)
" Plug 'neoclide/coc.nvim', {'tag': '*', 'do': { -> coc#util#install()}}
call plug#end()
filetype plugin indent on
" }}}
" {{{ General Configures
syntax on
se bg=dark " Set the background before color
colo default  " deepin-terminal theme Ura is better
se bg=dark " After it as well
se nu ai cin sts=2 ts=2 sw=2 et tw=180 mouse=a bs=2 cul
se fdm=syntax " For C++ 
" se foldmethod=indent " For Python
"" Unfold all folds at startup
au BufRead * normal zR  
set showcmd	    	" Show (partial) command in status line.
"set showmatch		" Show matching brackets.
"set ignorecase		" Do case insensitive matching
set smartcase		" Do smart case matching
"set incsearch		" Incremental search
"set autowrite		" Automatically save before commands like :next and :make
"set hidden 		" Hide buffers when they are abandoned

set nowrap ss=0 " sidescroll
" set warp linebreak breakat-=_ showbreak=->
"" Navigate within wraps (Buggy)
"" Use gj and jk instead.
"noremap j (v:count == 0 ? 'gj' : 'j')
"noremap k (v:count == 0 ? 'gk' : 'k')
"nnoremap j gj
"nnoremap k gk


au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
" Conf end}}}
" {{{ Chinese Support
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,chinese,cp936
language messages zh_CN.utf-8
" }}}
" {{{ GUI configures
" set guifont=Cascadia\ Code\ PL\ Semi-Bold\ 14
" }}}
" {{{ No Littering
set noundofile
set nobackup
" }}}
" {{{ About the nerd commenter
" Add spaces after comment delimiters by default
let g:NERDSpaceDelims = 1
" Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs = 1
" Align line-wise comment delimiters flush left instead of following code indentation
let g:NERDDefaultAlign = 'left'
" Set a language to use its alternate delimiters by default
let g:NERDAltDelims_java = 1
" Add your own custom formats or override the defaults
" let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/' } }
" Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDCommentEmptyLines = 1
" Enable trimming of trailing whitespace when uncommenting
let g:NERDTrimTrailingWhitespace = 1
" Enable NERDCommenterToggle to check all selected lines is commented or not 
let g:NERDToggleCheckAllLines = 1
" }}}
" {{{ Fly with Kite
" set statusline=%<%f\ %h%m%r%{kite#statusline()}%=%-14.(%l,%c%V%)\ %P
set laststatus=2  " always display the status line
" let g:kite_tab_complete=1
" let g:kite_supported_languages = ['*']
" }}}
" {{{ Follow the Leader
let mapleader=","
nn <leader>w :wa<CR>
nn <leader>i :call Initcpp()<CR>
" nnoremap wk :WakaTimeToday<CR>
" }}}
" {{{ User(dandelight) Defined Shortcuts & Language Specific Maps
fun Build(lang) " {{{
  wa
  vs
  wincmd l
  if a:lang ==? "cpp"
    !g++ -std=c++17 -Wall -Wextra -Wshadow -pedantic -O2 %
    term ./a.out
  elseif a:lang ==? "kt"
    !kotlinc -include-runtime -d out.jar %
    term java -jar out.jar
  elseif a:lang ==? "c"
    !gcc -Wall -O2 -Wextra -Wshadow -pendantic %
    term ./a.out
  elseif a:lang ==? "java"
    !javac %
    tarm java %:r
  elseif a:lang ==? "tex"
    !xelatex % && google-chrome %:r.pdf
  else
    echoerr "Unrecognized File: ".a:lang
  endif
endf
" }}}
fun Debug(lang) " {{{
  wa
  vs
  wincmd l
  if a:lang ==? "cpp"
    !g++ -std=c++17 -Wall -g -Wextra -Wshadow -pedantic %
    term gdb -q a.out
  elseif a:lang ==? "c"
    !gcc -Wall -g -Wextra -Wshadow -pendantic %
    term gdb -q a.out
  else
    echoerr "Unrecognized File: ".a:lang
  endif
endf
" }}}
fun! Run(lang) " {{{
  wa
  vs
  wincmd l
  if a:lang ==? "py"
    term python3 -i %
  elseif a:lang ==? "js"
    term node %
  else
    echoerr "Unrecognized File: ".a:lang
  endif
endf
" }}}
func Initcpp() " {{{
  let l = 0
  let l = l + 1 | call setline(l,'#include<bits/stdc++.h>') 
  let l = l + 1 | call setline(l,'using namespace std;')
  let l = l + 1 | call setline(l,'int solve() {')
  let l = l + 1 | call setline(l,' ')
  let l = l + 1 | call setline(l,'  return 0;')
  let l = l + 1 | call setline(l,'}') 
  let l = l + 1 | call setline(l,'int main() {') 
  let l = l + 1 | call setline(l,'  ios_base::sync_with_stdio(0);')
  let l = l + 1 | call setline(l,'  cin.tie(0); cout.tie(0);')
  let l = l + 1 | call setline(l,'  // freopen("in.txt","r",stdin);')
  let l = l + 1 | call setline(l,'  // freopen("out.txt","w",stdout);')
  let l = l + 1 | call setline(l,'  int tc; tc = 1;')
  let l = l + 1 | call setline(l,'  for(int t = 1; t <= tc; t++) {')
  let l = l + 1 | call setline(l,'    // cout << "Case #" << t << ": \n";')
  let l = l + 1 | call setline(l,'    solve();')
  let l = l + 1 | call setline(l,'  }')
  let l = l + 1 | call setline(l,'  return 0;') 
  let l = l + 1 | call setline(l,'}')
  exec "4"
endfunc
" }}}
" {{{ Language Support
"c
au FileType c nn <buffer> <F5> :call Debug("c")<CR>
au FileType c nn <buffer> <F6> :call Build("c")<CR>
"cpp
au FileType cpp nn <buffer> <F6> :call Build("cpp")<CR>
au FileType cpp nn <buffer> <F5> :call Debug("cpp")<CR>
"JavaScript
 au FileType javascript nn <buffer> <F6> :call Run("js")<CR>
"Python
au FileType python nn <buffer> <F6> :call Run("py")<CR>
"Java
au FileType java nn <F6> :call Build("java")
"LaTeX
au FileType tex nn <F6> :call Build("tex")
au FileType tex nn <F7> :!google-chrome %:r.pdf&<cr><cr>
"Kotlin
au FileType kotlin nn <buffer> <F6> :call Build("kt")<CR>
"Vim
au FileType vim setlocal fdm=marker
au FileType vim nn <buffer> <F6> :source %<CR>
" }}}
" }}}
" {{{ Key map
nn > >>
nn < <<
ino <c-]> {<CR>}<esc>O
" set backspace=indent,eol,start
" }}}
" au UserGettingBored
